package com.dk.controller;

import com.dk.pojo.Menu;
import com.dk.pojo.User;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menuController")
public class MenuController {

        @Autowired
        MenuService menuService;


        @RequestMapping("getMenu")
        @ResponseBody
        public List<Map<String,Object>> getMenu(HttpServletRequest request){
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("userLogin");
            List<Map<String,Object>> menu =  menuService.getMenu(user);
            return menu;
        }

        @RequestMapping("toZTree")
        public String toZTree(){
            return "menu/show";
        }
        @RequestMapping("getzTree")
        @ResponseBody
        public List<Map<String,Object>> getzTree(){
            List<Map<String,Object>> zTree =menuService.getzTree();
            return zTree;
        }

        @RequestMapping("toInsert")
        public String toInsert(){
            return "menu/add";
        }
        @RequestMapping("addMenuInfo")
        @ResponseBody
        public boolean addMenuInfo(Menu menu){
            int i = menuService.addMenuInfo(menu);
            return i>0;
        }

        @RequestMapping("toUpdate/{nodeId}")
        public String toUpdate(@PathVariable("nodeId")Integer nodeId, Model model){
            Menu menu = menuService.getMenuById(nodeId);
            model.addAttribute("menu",menu);
            return "menu/update";
        }
        @RequestMapping("updateMenuById")
        @ResponseBody
        public boolean updateMenuById(Menu menu){
            int i = menuService.updateMenuById(menu);
            return i>0;
        }

        @RequestMapping("deleteMenuById/{nodeId}")
        @ResponseBody
        public boolean deleteMenuById(@PathVariable("nodeId") Integer nodeId){
            int i = menuService.deleteMenuById(nodeId);
            return i>0;
        }
        @RequestMapping("getzTreeByRid")
        @ResponseBody
        public List<Map<String,Object>> getzTreeByRid(Integer rid){
            List<Map<String,Object>> zTree =menuService.getzTreeByRid(rid);
            return zTree;
        }




    }
